﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyData.ExpressionBuilder.Model;
using MyData.ExpressionBuilder.BaseExtensions;
using sp = MyData.SharePoint;
using MyData.SharePoint;
using System.Activities.Presentation.Model;
using System.Windows.Forms;
using Microsoft.VisualBasic.Activities;
using System.Activities;
using System.Diagnostics;
using MyData.Activities;
using System.IO;
using System.Data.SqlClient;
using System.Activities.Statements;
using System.Collections.ObjectModel;
using MyData.Activities.SQLAzure;

namespace MyData.Extensions
{
  [ExpressionBuilderMetadata(Name = "SPFieldsItemsProvider", Description = "", ImagePath = "")]
  public class SPFieldsItemsProvider : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      ModelItem ownerActivity = (from m in metadata where m.Name == "OwnerActivity" select m.Value).First() as ModelItem;

      ModelItem baseActivity = ownerActivity.BaseActivity("SharePointBaseActivity");
      string url = string.Empty;
      string title = string.Empty;
      
      if (ownerActivity != null)
      {
        url = baseActivity.Expression<string>("Url").ToString();
        title = baseActivity.Expression<string>("ListTitle").ToString();
      }

      sp.SharePointItemFields fields = new sp.SharePointItemFields(url, title);
      return fields.Items;
    }
  }

  [ExpressionBuilderMetadata(Name = "SqlViewNamesItemsProvider", Description = "", ImagePath = "")]
  public class SqlViewNamesItemsProvider : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      ModelItem ownerActivity = (from m in metadata where m.Name == "OwnerActivity" select m.Value).First() as ModelItem;
      SqlConnectionStringBuilder sqlConnectionBuilder = (from m in metadata where m.Name == "ConnectionStringBuilder" select m.Value).First() as SqlConnectionStringBuilder;
      
      if (ownerActivity != null)
      {
        //This version uses a Sequence activity to get DataSource and InitialCatalog. A future release will use a custom SQLSequence : Sequence activity
        ModelItem mi = ownerActivity.SequenceActivity("SqlConnectionBuilder");
        if (mi.ItemType == typeof(Sequence))
        {
          Collection<Activity> computedValues = mi.Properties["Activities"].ComputedValue as Collection<Activity>;

          ConnectionBuilderActivity connectionBuilderActivity = 
            (from m in computedValues where m.GetType() == typeof(ConnectionBuilderActivity) select m).First() as ConnectionBuilderActivity;

          if (connectionBuilderActivity != null)
          {
            sqlConnectionBuilder.DataSource = @connectionBuilderActivity.DataSource.Expression.ToString();
            sqlConnectionBuilder.InitialCatalog = connectionBuilderActivity.InitialCatalog.Expression.ToString();
          }
        }
      }

      return MyData.Activities.Common.ViewNames(sqlConnectionBuilder);
    }
  }

  //NEXT: Duplicate code to SqlViewNamesItemsProvider
  [ExpressionBuilderMetadata(Name = "SqlViewSchemaItemsProvider", Description = "", ImagePath = "")]
  public class SqlViewSchemaItemsProvider : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      ModelItem ownerActivity = (from m in metadata where m.Name == "OwnerActivity" select m.Value).First() as ModelItem;

      SqlConnectionStringBuilder sqlConnectionBuilder = 
        (from m in metadata where m.Name == "ConnectionStringBuilder" select m.Value).First() as SqlConnectionStringBuilder;

      if (ownerActivity != null)
      {
        //This version uses a Sequence activity to get DataSource and InitialCatalog. A future release will use a custom SQLSequence : Sequence activity
        ModelItem mi = ownerActivity.SequenceActivity("SqlConnectionBuilder");
        if (mi.ItemType == typeof(Sequence))
        {
          Collection<Activity> computedValues = mi.Properties["Activities"].ComputedValue as Collection<Activity>;

          ConnectionBuilderActivity connectionBuilderActivity =
            (from m in computedValues where m.GetType() == typeof(ConnectionBuilderActivity) 
             select m).First() as ConnectionBuilderActivity;

          if (connectionBuilderActivity != null)
          {
            sqlConnectionBuilder.DataSource = @connectionBuilderActivity.DataSource.Expression.ToString();
            sqlConnectionBuilder.InitialCatalog = connectionBuilderActivity.InitialCatalog.Expression.ToString();
          }
        }
      }

      return MyData.Activities.Common.ViewSchema(ownerActivity.Expression<string>("SqlViewName").ToString(), sqlConnectionBuilder);
    }
  }

  [ExpressionBuilderMetadata(Name = "TestViewItemsProvider", Description = "", ImagePath = "")]
  public class ViewItems : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      return new List<ListItem> 
      { 
        new ListItem { Name = "Sheet1", Value="Sheet1"}, 
        new ListItem { Name = "Sheet2", Value="Sheet2"} 
      };
    }
  }

  //VariableItemsProvider
  [ExpressionBuilderMetadata(Name = "VariableItemsProvider", Description = "", ImagePath = "")]
  public class VariableItems : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      List<ListItem> items = new List<ListItem>();
      
      List<string> variableNames = (from m in metadata where m.Name == "VariableNames" 
                                    select m.Value).First() as List<string>;

      foreach (var v in variableNames)
      {
        items.Add(new ListItem { Name = v, Value = v });
      }

      return items;
    }
  }

  [ExpressionBuilderMetadata(Name = "ViewNamesItemsProvider", Description = "", ImagePath = "")]
  public class ViewNames : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      ModelItem ownerActivity = (from m in metadata where m.Name == "OwnerActivity" 
                                 select m.Value).First() as ModelItem;

      SqlConnectionStringBuilder connStringBuilder = (SqlConnectionStringBuilder)ownerActivity
        .Expression<SqlConnectionStringBuilder>("SqlConnectionBuilder");

      return MyData.Activities.Common.ViewNames(connStringBuilder);
    }
  }

  [ExpressionBuilderMetadata(Name = "ReferenceItemsProvider", Description = "", ImagePath = "")]
  public class ReferenceItems : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      ModelItem ownerActivity = (from m in metadata where m.Name == "OwnerActivity" 
                                 select m.Value).First() as ModelItem;
      ModelItem baseActivity = ownerActivity.BaseActivity("SharePointBaseActivity");
      string documentName = baseActivity.Expression<string>("DocumentName").ToString();

      List<ListItem> list = null;
      
      if (ownerActivity != null)
      {
        //NOTE: Excel provider is hard-coded for example
        if (Path.GetExtension(documentName) == ".docx")
        {
          var contentControls = MyData.Activities.Common.ContentControls
            (baseActivity.Expression<string>("Url").ToString(),
            documentName,
            baseActivity.Expression<string>("LibraryTitle").ToString());
          
          list = contentControls.Where(c => c.IsEmbedded == false).Where(c => c.HasTable == false)
              .Select(c => new ListItem { Name = c.Name, Value = c }).ToList<ListItem>();

        }
        else if (Path.GetExtension(documentName) == ".xlsx")
        {
          list = new List<ListItem> 
          { new ListItem { Name = "A1"}, 
            new ListItem { Name = "A2"},
            new ListItem { Name = "A3"},
          };
        }
      }

      return list;
    }
  }

  [ExpressionBuilderMetadata(Name = "TestItemsProvider", Description = "", ImagePath = "")]
  public class TestItemsProvider : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      return new List<ListItem> 
      { 
        new ListItem { Name = "Title"}, 
        new ListItem { Name = "Body"},
        new ListItem { Name = "Status"}
      };
    }
  }

  [ExpressionBuilderMetadata(Name = "PartTestItemsProvider", Description = "", ImagePath = "")]
  public class PartTestItemsProvider : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      return new List<ListItem> 
      { 
        new ListItem { Name = "Announcements", Value="Announcements"}, 
        new ListItem { Name = "Tasks", Value="Tasks"},
        new ListItem { Name = "Test", Value="Test"}
      };
    }
  }

  [ExpressionBuilderMetadata(Name = "SharePointListItems", Description = "Returns SharePoint ListItems", ImagePath = "")]
  public class SharePointListItems : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      ModelItem ownerActivity = (from m in metadata 
                                 where m.Name == "OwnerActivity" 
                                 select m.Value).First() as ModelItem;
      ModelItem baseActivity = ownerActivity.BaseActivity("SharePointBaseActivity");
      string url = string.Empty;
      if (baseActivity != null)
      {
        url = baseActivity.Expression<string>("Url").ToString();
      }

      string path = (from m in metadata select m).ToList()[1].Value as string;  
      MyData.SharePoint.SharePointItems spItems = new MyData.SharePoint.SharePointItems(url, path);
      return spItems.Items;
    }
  }

  [ExpressionBuilderMetadata(Name = "ColorItemsProvider", Description = "", ImagePath = "")]
  public class ColorItemsProvider : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      return new List<ListItem> 
      { 
        new ListItem { Name = "Normal", Value="Normal"}, 
        new ListItem { Name = "Bad", Value="Bad"},
        new ListItem { Name = "Good", Value="Good"},
        new ListItem { Name = "Neutral", Value="Neutral"}
      };
    }
  }
}
